//2908.元素和最小的山形三元组I
//https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/?envType=daily-question&envId=2024-03-29
class Solution {
public:
    int minimumSum(vector<int>& nums) {
        int n = nums.size();
        int prevmin = INT_MAX;
        int ret = INT_MAX;

        //lmin[n] 表示前n个数（不包括n）中最小的 
        vector<int> lmin(n);

        //前n-1个中最小的
        for(int i = 1;i<n;++i) lmin[i] = prevmin = min(prevmin,nums[i-1]);
        
        //当前的rmin表示后n个中最小的
        int rmin = nums[n-1];
        for(int i = n-2;i>0;--i)
        {
            if(lmin[i] < nums[i] && nums[i] > rmin) ret = min(ret,lmin[i]+nums[i]+rmin);
            rmin = min(rmin,nums[i]);
        }
        return ret!=INT_MAX?ret:-1;
    }
};